<?php
namespace Admin\Controller;
use Admin\Model\OrderModel;
use Think\Page;
use Common\Model\UserModel;
use Common\Model\UserInfoModel;
use Admin\Model\OrderProjectModel;
use Admin\Model\OrderRebackModel;

/**
 * 销售订单控制器
 * 
 * @author xiaoyutab<xiaoyutab@qq.com>
 * @version v1.0.0
 * @copyright (c) 2017, xiaoyutab
 * @adtime 2018-06-01 17:50:24
 */
class OrderController extends BashController{
    /**
     * 销售订单控制器
     * 
     * @return void
     * @author xiaoyutab<xiaoyutab@qq.com>
     * @copyright (c) 2017, xiaoyutab
     * @version v1.0.0
     * @adtime 2018-06-01 17:50:24
     */
    public function xunAction(){
        $_order = new OrderModel();
        $_user = new UserModel();
        $_user_info = new UserInfoModel();
        $where = [];
        $ids = [];
        foreach(explode(' ', I('get.keywords')) as $v){
            $where[] = '%'.$v.'%';
        }
        $list = $_user->where(['username'=>['like',$where]])->select();
        foreach($list as $v){
            $ids[] = $v['id'];
        }
        $list = $_user_info->where(['nickname'=>['like',$where]])->select();
        foreach($list as $v){
            $ids[] = $v['id'];
        }
        $wheres = [];
        $wheres[0] = array(
            'order_no|name|composer' => array('like',$where),
        );
        if(!empty($ids)){
            $wheres[0]['uid'] = ['in',$ids];
            $wheres[0]['_logic'] = 'or';
        }
        $wheres['type'] = 1;
        $wheres[1]['status'] = ['notin',[98,97,5]];
        $count = $_order->where($wheres)->getCount();
        $page = new Page($count, 10);
        $list = $_order->where($wheres)->getList($page->firstRow, $page->listRows);
        return $this->assign(array(
            'count' => $count,
            'list' => $list,
            'page' => $page->show(),
        ))->display();
    }
    
    /**
     * 销售完成订单控制器
     * 
     * @return void
     * @author xiaoyutab<xiaoyutab@qq.com>
     * @copyright (c) 2017, xiaoyutab
     * @version v1.0.0
     * @adtime 2018-06-29 23:05:41
     */
    public function xun_oAction(){
        $_order = new OrderModel();
        $_user = new UserModel();
        $_user_info = new UserInfoModel();
        $where = [];
        $ids = [];
        foreach(explode(' ', I('get.keywords')) as $v){
            $where[] = '%'.$v.'%';
        }
        $list = $_user->where(['username'=>['like',$where]])->select();
        foreach($list as $v){
            $ids[] = $v['id'];
        }
        $list = $_user_info->where(['nickname'=>['like',$where]])->select();
        foreach($list as $v){
            $ids[] = $v['id'];
        }
        $wheres = [];
        $wheres[0] = array(
            'order_no|name|composer' => array('like',$where),
        );
        if(!empty($ids)){
            $wheres[0]['uid'] = ['in',$ids];
            $wheres[0]['_logic'] = 'or';
        }
        $wheres['type'] = 1;
        $wheres[1]['status'] = 5;
        $count = $_order->where($wheres)->getCount();
        $page = new Page($count, 10);
        $list = $_order->where($wheres)->getList($page->firstRow, $page->listRows);
        return $this->assign(array(
            'count' => $count,
            'list' => $list,
            'page' => $page->show(),
        ))->display();
        return $this->display();
    }
    
    public function xun_editAction(){
        $_order = new OrderModel();
        $_order_project = new OrderProjectModel();
        $info = $_order->get_info(I('get.id'));
        $info['project'] = $_order_project->where(['oid'=>$info['id']])->select();
        $this->assign('order',$info);
        return $this->display();
    }
    
    /**
     * 设置忽略订单
     * @return type
     */
    public function xun_hlAction(){
        $_order = new OrderModel();
        $_order->where(['id'=>I('get.id')])->save(['status'=>97]);
//        if($_order->where(['id'=>I('get.id')])->save(['status'=>97])){
            return $this->success('忽略成功',U('xun'));
//        }
//        return $this->error('系统错误');
    }
    
    /**
     * 待付款订单
     * 
     * @return void
     * @author xiaoyutab<xiaoyutab@qq.com>
     * @copyright (c) 2017, xiaoyutab
     * @version v1.0.0
     * @adtime 2018-06-29 23:05:41
     */
    public function xun_financeAction(){
        $_order = new OrderModel();
        $_user = new UserModel();
        $_user_info = new UserInfoModel();
        $where = [];
        $ids = [];
        foreach(explode(' ', I('get.keywords')) as $v){
            $where[] = '%'.$v.'%';
        }
        $list = $_user->where(['username'=>['like',$where]])->select();
        foreach($list as $v){
            $ids[] = $v['id'];
        }
        $list = $_user_info->where(['nickname'=>['like',$where]])->select();
        foreach($list as $v){
            $ids[] = $v['id'];
        }
        $wheres = [];
        $wheres[0] = array(
            'order_no|name|composer' => array('like',$where),
        );
        if(!empty($ids)){
            $wheres[0]['uid'] = ['in',$ids];
            $wheres[0]['_logic'] = 'or';
        }
        $wheres['type'] = 1;
        $wheres[1]['status'] = ['in',[1,5,4]];
        $count = $_order->where($wheres)->getCount();
        $page = new Page($count, 10);
        $list = $_order->where($wheres)->getList($page->firstRow, $page->listRows);
        return $this->assign(array(
            'count' => $count,
            'list' => $list,
            'page' => $page->show(),
        ))->display();
        return $this->display();
    }
    
    /**
     * 确认订单已支付完成
     * @return type
     */
    public function finance_zhifuAction(){
        $_order = new OrderModel();
        $_order->where(['id'=>I('get.id')])->save(['status'=>6]);
//        if($_order->where(['id'=>I('get.id')])->save(['status'=>6])){
        return $this->success('支付成功',U('xun_finance'));
//        }
//        return $this->error('系统错误');
    }
    
    /**
     * 已付款订单
     * 
     * @return void
     * @author xiaoyutab<xiaoyutab@qq.com>
     * @copyright (c) 2017, xiaoyutab
     * @version v1.0.0
     * @adtime 2018-06-29 23:05:41
     */
    public function xun_finance_oAction(){
        $_order = new OrderModel();
        $_user = new UserModel();
        $_user_info = new UserInfoModel();
        $where = [];
        $ids = [];
        foreach(explode(' ', I('get.keywords')) as $v){
            $where[] = '%'.$v.'%';
        }
        $list = $_user->where(['username'=>['like',$where]])->select();
        foreach($list as $v){
            $ids[] = $v['id'];
        }
        $list = $_user_info->where(['nickname'=>['like',$where]])->select();
        foreach($list as $v){
            $ids[] = $v['id'];
        }
        $wheres = [];
        if(I('get.keywords') == '已签收'){
            $where['is_shouhuo'] = 2;
        } else {
            $wheres[0] = array(
                'order_no|name|composer|phone' => array('like',$where),
            );
            if(!empty($ids)){
                $wheres[0]['uid'] = ['in',$ids];
                $wheres[0]['_logic'] = 'or';
            }
        }
        $wheres['type'] = 1;
        $wheres[1]['status'] = ['in',[3,6,99]];
        $count = $_order->where($wheres)->getCount();
        $page = new Page($count, 10);
        $list = $_order->where($wheres)->getList($page->firstRow, $page->listRows);
        return $this->assign(array(
            'count' => $count,
            'list' => $list,
            'page' => $page->show(),
        ))->display();
    }
    
    /**
     * 售后订单列表
     * @return type
     */
    public function xun_finance_rAction(){
        $_order_reback = new OrderRebackModel();
        $_order = new OrderModel();
        $_order_project = new OrderProjectModel();
        $_user = new UserModel();
        $_user_info = new UserInfoModel();
        $where = [];
        $uids = [];
        $oids = [];
        $pids = [];
        foreach(explode(' ', I('get.keywords')) as $v){
            $where[] = '%'.$v.'%';
        }
        $list = $_user->where(['username'=>['like',$where]])->select();
        foreach($list as $v){
            $uids[] = $v['id'];
        }
        $list = $_user_info->where(['nickname'=>['like',$where]])->select();
        foreach($list as $v){
            $uids[] = $v['id'];
        }
        $wheres = [];
        $wheres[0] = array(
            'content' => array('like',$where),
        );
        if(!empty($uids)){
            $wheres[0]['uid'] = ['in',$uids];
        }
        $list = $_order->where(array(
            'order_no|name|composer' => array('like',$where),
        ))->select();
        foreach($list as $v){
            $oids[] = $v['id'];
        }
        if(!empty($oids)){
            $wheres[0]['order_id'] = ['in',$oids];
        }
        $list = $_order_project->where(['name|wl_no|content'=>['like',$where]])->select();
        foreach($list as $v){
            $pids[] = $v['id'];
        }
        if(!empty($pids)){
            $wheres[0]['order_project_id'] = ['in',$pids];
        }
        $wheres[0]['_logic'] = 'or';
        $wheres[1]['status'] = 1;
        $count = $_order_reback->where($wheres)->getCount();
        $page = new Page($count, 10);
        $list = $_order_reback->where($wheres)->getList($page->firstRow, $page->listRows);
        foreach($list as $k => $v){
            $v['project'] = $_order_project->get_info($v['order_project_id']);
            $v['order'] = $_order->get_info($v['order_id']);
            $list[$k] = $v;
        }
        return $this->assign(array(
            'count' => $count,
            'list' => $list,
            'page' => $page->show(),
        ))->display();
    }
    
    /**
     * 查看订单信息
     * 
     * @return type
     */
    public function finance_lookAction(){
        $id = I('get.id','','intval');
        $_order_reback = new OrderRebackModel();
        $_order = new OrderModel();
        $_order_project = new OrderProjectModel();
        $_user = new UserModel();
        $reback = $_order_reback->get_info($id);
        $reback['statu_name'] = $_order_reback->get_config($reback['statu'],'status');
        $reback['type_name'] = $_order_reback->get_config($reback['type'],'type');
        $this->assign([
            'order' => $_order->get_info($reback['order_id']),
            'project' => $_order_project->get_info($reback['order_project_id']),
            'user' => $_user->get_info($reback['uid']),
            'reback' => $reback
        ]);
        return $this->display();
    }
    
    /**
     * 确认订单已支付完成
     * @return type
     */
    public function finance_rebackAction(){
        $_order_reback = new OrderRebackModel();
        $_order_reback->where(['id'=>I('get.id')])->save(['status'=>I('get.status')]);
//        if($_order_reback->where(['id'=>I('get.id')])->save(['status'=>I('get.status')])){
        return $this->success('操作成功',U('xun_finance_r'));
//        }
//        return $this->error('系统错误');
    }
    
    /**
     * 售后已处理订单列表
     * @return type
     */
    public function xun_finance_rcAction(){
        $_order_reback = new OrderRebackModel();
        $_order = new OrderModel();
        $_order_project = new OrderProjectModel();
        $_user = new UserModel();
        $_user_info = new UserInfoModel();
        $where = [];
        $uids = [];
        $oids = [];
        $pids = [];
        foreach(explode(' ', I('get.keywords')) as $v){
            $where[] = '%'.$v.'%';
        }
        $list = $_user->where(['username'=>['like',$where]])->select();
        foreach($list as $v){
            $uids[] = $v['id'];
        }
        $list = $_user_info->where(['nickname'=>['like',$where]])->select();
        foreach($list as $v){
            $uids[] = $v['id'];
        }
        $wheres = [];
        $wheres[0] = array(
            'content' => array('like',$where),
        );
        if(!empty($uids)){
            $wheres[0]['uid'] = ['in',$uids];
        }
        $list = $_order->where(array(
            'order_no|name|composer' => array('like',$where),
        ))->select();
        foreach($list as $v){
            $oids[] = $v['id'];
        }
        if(!empty($oids)){
            $wheres[0]['order_id'] = ['in',$oids];
        }
        $list = $_order_project->where(['name|wl_no|content'=>['like',$where]])->select();
        foreach($list as $v){
            $pids[] = $v['id'];
        }
        if(!empty($pids)){
            $wheres[0]['order_project_id'] = ['in',$pids];
        }
        $wheres[0]['_logic'] = 'or';
        $wheres[1]['status'] = ['neq',1];
        $count = $_order_reback->where($wheres)->getCount();
        $page = new Page($count, 10);
        $list = $_order_reback->where($wheres)->getList($page->firstRow, $page->listRows);
        foreach($list as $k => $v){
            $v['project'] = $_order_project->get_info($v['order_project_id']);
            $v['order'] = $_order->get_info($v['order_id']);
            $list[$k] = $v;
        }
        return $this->assign(array(
            'count' => $count,
            'list' => $list,
            'page' => $page->show(),
        ))->display();
    }
    
    /**
     * 订单发货
     * @return type
     */
    public function finance_fahuoAction(){
        $_order = new OrderModel();
        $_order->where(['id'=>I('get.id')])->save([
            'kd_name'=>I('post.kd_name'),
            'kd_no'=>I('post.kd_no'),
        ]);
//        if($_order->where(['id'=>I('get.id')])->save([
//            'kd_name'=>I('post.kd_name'),
//            'kd_no'=>I('post.kd_no'),
//        ])){
            return $this->redirect('xun_finance_o');
//        }
//        return $this->error('系统错误');
    }
}